#include "PHYModeler.h"
#include "PHYView.h"
#include "PHYFactory.h"
#include "PHYCallback.h"
#include <osgGA/TrackballManipulator>
#include <osg/PositionAttitudeTransform>
#include <osg/Group>
#include <osg/Node>
#include <osg/Geometry>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/ShapeDrawable>

PHYView::PHYView()
{
	pModeler = PHYModeler::getInstance();
}
PHYView::~PHYView()
{
	delete pModeler;
}
PHYModeler *PHYView::getModeler()
{
	return pModeler;
}
bool PHYView::CreateScene()
{
	cout << "PHYView::CreateScene()" << endl;
	osg::Group* root = new osg::Group();
	PHYVector orig(0,0,0), vec1(0,20,0), vec2(0,-30,0), vec3(0,20,20);
	PHYFactory *pFact = new PHYFactory();
	PHYCollisionObj* pSphere1 = NULL, *pGround = NULL, *pSphere2 = NULL, *pConvex = NULL;
	if(pFact)
	{
		//pSphere1 = pFact->CreateSphere(1, vec1);
		//pSphere2 = pFact->CreateSphere(2, vec3);
		pGround = pFact->CreateBox(100, 1, 100, vec2);
		pConvex = pFact->CreateConvex("H:/models/ModelComp/one.obj",vec3);
	}
	if(pSphere1)
		root->addChild(pSphere1->getTransform());
	if(pSphere2)
		root->addChild(pSphere2->getTransform());
	if(pGround)
		root->addChild(pGround->getTransform());
	if(pConvex)
		root->addChild(pConvex->getTransform());
	
	root->setUpdateCallback(new PHYCollisionObjCallback());
	
	pModeler->BeginSimulation();

	osgViewer::Viewer viewer;

	viewer.setSceneData( root );
	viewer.setCameraManipulator(new osgGA::TrackballManipulator());

	double simulationtime = 0.;
	viewer.realize();
	while( !viewer.done() )
	{
		simulationtime = simulationtime + .05;
		viewer.frame(simulationtime);
		//viewer.requestRedraw();
	}

	//viewer.run();
	delete pFact;
	return 1;
}
